package ae.pcfc.etks.online.itworx.ehs.web.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.validator.constraints.Length;

@Entity
@Table(name = "T_EHS_COMMENT")
@SequenceGenerator(name = "EHS_COMMENT_ID_SEQ", sequenceName = "EHS_COMMENT_ID_SEQ", initialValue = 1, allocationSize = 1)
public class Comment extends AuditableEntity implements Serializable {
	
	@Id
	@Column(name = "EHS_COMMENT_ID")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EHS_COMMENT_ID_SEQ")
	private long id;

	@Column(name = "COMMENT_DETAILS")
	private String commentDetails;
	
	@Length(max=500)
	@Column(name = "REPLY")
	private String reply;
	
	@Column(name = "IMS_DEPT_ID")
	private String imsDeptId;
	
	@Column(name = "IMS_INSPECTOR_NAME")
	private String imsInspectorName;
	
	@Column(name = "IMS_INSPECTOR_NUMBER")
	private Integer imsInspectorNumber;
	
	@Column(name = "IMS_COMMENT_SEQNO")
	private Integer imsCommentSeqNo;
	
	@Column(name = "IMS_SEVERITY")
	private String imsSeverity;
	
	@Column(name = "COMMENT_DATE")
	private Date commentDate;
	
	//@Column(name = "REPLIED", nullable=false)
	@Column(name = "REPLIED")
	private Boolean replied = false;
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getCommentDetails() {
		return commentDetails;
	}

	public void setCommentDetails(String commentDetails) {
		this.commentDetails = commentDetails;
	}

	public String getReply() {
		return reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}

	public String getImsDeptId() {
		return imsDeptId;
	}

	public void setImsDeptId(String imsDeptId) {
		this.imsDeptId = imsDeptId;
	}

	public String getImsInspectorName() {
		return imsInspectorName;
	}

	public void setImsInspectorName(String imsInspectorName) {
		this.imsInspectorName = imsInspectorName;
	}

	public Integer getImsInspectorNumber() {
		return imsInspectorNumber;
	}

	public void setImsInspectorNumber(Integer imsInspectorNumber) {
		this.imsInspectorNumber = imsInspectorNumber;
	}

	public Integer getImsCommentSeqNo() {
		return imsCommentSeqNo;
	}

	public void setImsCommentSeqNo(Integer imsCommentSeqNo) {
		this.imsCommentSeqNo = imsCommentSeqNo;
	}

	public String getImsSeverity() {
		return imsSeverity;
	}

	public void setImsSeverity(String imsSeverity) {
		this.imsSeverity = imsSeverity;
	}

	public Date getCommentDate() {
		return commentDate;
	}

	public void setCommentDate(Date commentDate) {
		this.commentDate = commentDate;
	}
	
	public Boolean getReplied() {
		//if(replie)
		return replied;
	}

	public void setReplied(Boolean replied) {
		//if(String.valueOf(replied) == null){
		//	replied = false;
		//}
		this.replied = replied; //==null?false:replied;
	}

	public boolean imsSameKey(Comment other) {
		if (this == other)
			return true;
		if (other == null)
			return false;
		if (imsDeptId == null) {
			if (other.imsDeptId != null)
				return false;
		} else if (!imsDeptId.equals(other.imsDeptId))
			return false;
/*		if (imsInspectorNumber == null) {
			if (other.imsInspectorNumber != null)
				return false;
		} else if (!imsInspectorNumber.equals(other.imsInspectorNumber))
			return false;*/
		if (imsCommentSeqNo == null) {
			if (other.imsCommentSeqNo != null)
				return false;
		} else if (!imsCommentSeqNo.equals(other.imsCommentSeqNo))
			return false;
		return true;
	}

	
	public boolean updateValues(Comment other) {
		if (this == other)
			return false;
		
		if (other == null)
			return false;
		boolean updated = false;
		
		if(other.commentDate != null){
			if (commentDate == null 
					|| (!other.commentDate.equals(commentDate))){
				commentDate = other.commentDate;
				updated = true;
			}
		}
		
		if(other.commentDetails != null){
			if (commentDetails == null
					|| (!other.commentDetails.equals(commentDetails))){
				commentDetails = other.commentDetails;
				updated = true;	
			}
		}
		
		if(other.imsInspectorNumber != null){
			if (imsInspectorNumber == null
					|| (!other.imsInspectorNumber.equals(imsInspectorNumber))){
				imsInspectorNumber = other.imsInspectorNumber;
				updated = true;	
			}
		}
		
		if(other.imsInspectorName != null){
			if (imsInspectorName == null
					|| (!other.imsInspectorName.equals(imsInspectorName))){
				imsInspectorName = other.imsInspectorName;
				updated = true;
			}
		}
		
		if(other.imsSeverity != null){
			if (imsSeverity == null
					|| (!other.imsSeverity.equals(imsSeverity))){
				imsSeverity = other.imsSeverity;
				updated = true;	
			}
		}
		
//		if ((reply == null && other.reply != null) || (!reply.equals(other.reply))){
//			reply = other.getReply();
//			updated = true;
//		}
		return updated;
	}

	public boolean isSameInspection(Comment other) {
		return (other.imsInspectorNumber.equals(imsInspectorNumber));
	}
}
